package com.szqhyxj.youxiangju.biz.push.service;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.szqhyxj.youxiangju.biz.push.AddDeviceEvent;

import org.json.JSONObject;

import java.util.Iterator;

import de.greenrobot.event.EventBus;

/**
 * Created by  on 2018/7/30.
 */

public class GeTuiIntentService extends GTIntentService{

    public static final String KEY_MESSAGEID = "messageId";

    public static final String KEY_ACTIONID = "action";

    public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpush.MESSAGE_RECEIVED_ACTION";

    Gson gson =new Gson();
    @Override
    public void onReceiveServicePid(Context context, int i){
    }

    @Override
    public void onReceiveClientId(Context context, String s){
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + s);
    }

    /**
     * 透传消息
     * @param context
     * @param gtTransmitMessage
     */
    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage){

        processCustomMessage(context,gtTransmitMessage);

    }

    @Override
    public void onReceiveOnlineState(Context context, boolean b){
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage){
        Log.d(TAG, "onReceiveCommandResult = " + gtCmdMessage.toString());
    }

    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage){
        Log.d(TAG, "onNotificationMessageArrived = " + gtNotificationMessage.getContent());
    }

    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage gtNotificationMessage){
        Log.d(TAG, "gtNotificationMessage = " + gtNotificationMessage.getContent());
    }


    //自定义消息广播发送
    private void processCustomMessage(final Context context, GTTransmitMessage gtTransmitMessage) {

        byte[] payload = gtTransmitMessage.getPayload();
        String appid = gtTransmitMessage.getAppid();
        String taskid = gtTransmitMessage.getTaskId();
        String messageid = gtTransmitMessage.getMessageId();
        String pkg = gtTransmitMessage.getPkgName();
        String cid = gtTransmitMessage.getClientId();

        // 第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
        boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);

        final Intent msgIntent = new Intent(MESSAGE_RECEIVED_ACTION);
        msgIntent.putExtra(KEY_MESSAGEID, messageid);

        if (payload != null) {
            String data = new String(payload);

            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(data);
            Log.d(TAG, "jsonObject = " + jsonObject);
            String key = jsonObject.getString("retCode");
            String value = jsonObject.getString("retMsg");

            if (key.equals("0000")){
                EventBus.getDefault().post(new AddDeviceEvent(true,value));
            }if (key.equals("0001")){
                EventBus.getDefault().post(new AddDeviceEvent(false,value));
            }




        }



    }
}
